package com.sportizz.sport;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import com.sportizz.model.sports;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.PorterDuff.Mode;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class SubMenu_sport extends ArrayAdapter<sports> {

	Context context;
	// List<String> data;

	private final List<sports> data;

	List<String> data_sport;
	DatabaseAdapter dataAdapter;

	public SubMenu_sport(Context applicationContext, List<sports> data_sub,
			List<String> data_sport) {
		super(applicationContext, R.layout.sport_menu_list, data_sub);
		this.context = applicationContext;
		this.data = data_sub;
		this.data_sport = data_sport;

		dataAdapter = new DatabaseAdapter(context);
	}

	static class ViewHolder {
		protected TextView menu_name;
		protected CheckBox check_sport;
		protected ImageView menu_img;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		LayoutInflater inflator = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		ViewHolder viewHolder = null;

		if (convertView == null) {
			convertView = inflator.inflate(R.layout.sport_menu_list, null);
			viewHolder = new ViewHolder();
			viewHolder.menu_name = (TextView) convertView
					.findViewById(R.id.Menu_name);
			viewHolder.check_sport = (CheckBox) convertView
					.findViewById(R.id.menu_follow);
			viewHolder.menu_img = (ImageView) convertView
					.findViewById(R.id.image_menu);

			convertView.setTag(viewHolder);

			viewHolder.check_sport
					.setOnClickListener(new View.OnClickListener() {
						public void onClick(View v) {
							CheckBox cb = (CheckBox) v;
							sports s = (sports) cb.getTag();
//							Toast.makeText(
//									context,
//									"Clicked on Checkbox: " + s.getName()
//											+ " is " + cb.isChecked(),
//									Toast.LENGTH_LONG).show();
							s.setSelected(cb.isChecked());

							if (cb.isChecked()) {

								data_sport.add(s.getName());

								dataAdapter.open();

								String sport_data = dataAdapter
										.getsportcodelist(s.getName());
								dataAdapter.insert_follow_info(sport_data,
										null, null, s.getName(), null, null,
										"s");
								dataAdapter.close();
							} else {
								data_sport.remove(s.getName());

								dataAdapter.open();
								String sport_data = dataAdapter
										.getsportcodelist(s.getName());
								dataAdapter.deletesportValue(sport_data, "s");
								dataAdapter.close();

							}

						}
					});

		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		sports sport = data.get(position);
		viewHolder.menu_name.setText(sport.getName());
		viewHolder.check_sport.setChecked(sport.isSelected());
		viewHolder.menu_img.setImageBitmap(bitmap_image((sport.getCode())));
		viewHolder.check_sport.setTag(sport);

		if (data_sport.contains(data.get(position).getName())) {
			viewHolder.check_sport.setChecked(true);
		} else {
			viewHolder.check_sport.setChecked(false);
		}
		return convertView;
	}

	private Bitmap bitmap_image(String image_code) {
		InputStream is1;
		Bitmap output1 = null;
		try {
			is1 = context.getResources().getAssets()
					.open("sports/" + image_code + ".png");

			Bitmap res1 = BitmapFactory.decodeStream(is1);

			Log.i("suraj", "champ =" + res1);

			output1 = Bitmap.createBitmap(res1.getWidth(), res1.getHeight(),
					Bitmap.Config.ARGB_8888);
			Canvas canvas1 = new Canvas(output1);

			Paint paint = new Paint();
			Rect rect = new Rect(0, 0, res1.getWidth(), res1.getHeight());
			RectF rectF = new RectF(rect);
			float roundPx1 = 8f;

			paint.setAntiAlias(true);
			canvas1.drawARGB(0, 0, 0, 0);
			paint.setColor(0xff424242);
			canvas1.drawRoundRect(rectF, roundPx1, roundPx1, paint);
			paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
			canvas1.drawBitmap(res1, rect, rect, paint);
		} catch (IOException e) {
			e.printStackTrace();
			try {
				is1 = context.getResources().getAssets()
						.open("sports/GAM.png");
			

			Bitmap res1 = BitmapFactory.decodeStream(is1);

			Log.i("suraj", "champ =" + res1);

			output1 = Bitmap.createBitmap(res1.getWidth(), res1.getHeight(),
					Bitmap.Config.ARGB_8888);
			Canvas canvas1 = new Canvas(output1);

			Paint paint = new Paint();
			Rect rect = new Rect(0, 0, res1.getWidth(), res1.getHeight());
			RectF rectF = new RectF(rect);
			float roundPx1 = 8f;

			paint.setAntiAlias(true);
			canvas1.drawARGB(0, 0, 0, 0);
			paint.setColor(0xff424242);
			canvas1.drawRoundRect(rectF, roundPx1, roundPx1, paint);
			paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
			canvas1.drawBitmap(res1, rect, rect, paint);
			
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		return output1;
	}

}
